#include "myhomepage.h"
#include "ui_myhomepage.h"

MyHomePage::MyHomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyHomePage)
{
    ui->setupUi(this);
    ui->pushButton_Light->setStyleSheet(QPushButton_Light_STYTLE_CLOSE);
    ui->pushButton_Light->setCheckable(true);
    ui->pushButton_Light->setText("照明关");
    connect(ui->pushButton_Light,SIGNAL(clicked()),this,SLOT(pushButtonLight()));
    ui->pushButton_jichuang->setStyleSheet(QPushButton_jichuang_STYTLE_CLOSE);
    ui->pushButton_jichuang->setCheckable(true);
    ui->pushButton_jichuang->setText("机床关");
    connect(ui->pushButton_jichuang,SIGNAL(clicked()),this,SLOT(pushButtonjichuang()));
    ui->pushButton_jingbao->setStyleSheet(QPushButton_jingbao_STYTLE_CLOSE);
    ui->pushButton_jingbao->setCheckable(true);
    ui->pushButton_jingbao->setText("警报关");
    connect(ui->pushButton_jingbao,SIGNAL(clicked()),this,SLOT(pushButtonjingbao()));
    ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_CLOSE);
    ui->pushButton_Fan->setCheckable(true);
    ui->pushButton_Fan->setText("风扇关");
    connect(ui->pushButton_Fan,SIGNAL(clicked()),this,SLOT(pushButtonFan()));
    connect(this,SIGNAL(ringSignal(int)),this,SLOT(ringSlot(int)));
    connect(this,SIGNAL(fireSignal(int)),this,SLOT(fireSlot(int)));
    connect(ui->pushButton_flash,SIGNAL(clicked()),this,SLOT(flashSlot()));
}

MyHomePage::~MyHomePage()
{
    delete ui;
}

void MyHomePage::dataUpSlot(const QByteArray& message)
{
    QString messageString = QString::fromUtf8(message);
    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(message,&jsonError);
    if (!jsonDoc.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        //qDebug() << "Received message:" << messageString;
        QJsonObject jsonObj = jsonDoc.object();

        // 访问顶层字段
        int type = jsonObj["type"].toInt();
        int result = jsonObj["result"].toInt();
        qDebug() <<"type:" <<type;
        qDebug()<<"result:" <<result;

        // 访问嵌套的JSON对象
        QJsonArray dataArray = jsonObj["data"].toArray();
           for (const QJsonValue &value : dataArray) {
               QJsonObject itemObj = value.toObject();
               int key = itemObj["key"].toInt();
               QString val = itemObj["val"].toString();
               int type=val.toInt();
               switch (key) {
               case 1:
                   emit ringSignal(type);
                   break;
               case 3:
                   ui->etcdata->setText(val);
                   break;
               case 4:
                   emit fireSignal(type);
                   break;
               case 5:
                   ui->temdata->setText(val);
                   break;
               case 6:
                   ui->humdata->setText(val);
               case 105:
                   ui->mecdata->setText(val);
                   break;
               default:
                   break;
               }
               //qDebug() << "Key:" << key << ", Value:" << val;
           }
      }
}
void MyHomePage::pushButtonLight()
{
    //QJsonObject root;
    if(ui->pushButton_Light->isChecked())
    {
        ui->pushButton_Light->setStyleSheet(QPushButton_Light_STYTLE_OPEN);
        ui->pushButton_Light->node.data_type="1";
        ui->pushButton_Light->setText("照明开");
    }
    else if(!ui->pushButton_Light->isChecked())
    {
        ui->pushButton_Light->setStyleSheet(QPushButton_Light_STYTLE_CLOSE);
        ui->pushButton_Light->node.data_type="0";
        ui->pushButton_Light->setText("照明关");
    }
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_Light->node.type=2;
    ui->pushButton_Light->node.key=9;

    node_data_data.insert("key",ui->pushButton_Light->node.key);
    node_data_data.insert("val",ui->pushButton_Light->node.data_type);
    node_data.insert("type",ui->pushButton_Light->node.type);
//    node_data.insert("key",ui->pushButton_Light->node.key);
//    node_data.insert("val",ui->pushButton_Light->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}

void MyHomePage::pushButtonjichuang()
{
    if(ui->pushButton_jichuang->isChecked())
    {
        ui->pushButton_jichuang->setStyleSheet(QPushButton_jichuang_STYTLE_OPEN);
        ui->pushButton_jichuang->setText("机床开");
        ui->pushButton_jichuang->node.data_type="1";
    }
    else if(!ui->pushButton_jichuang->isChecked())
    {
        ui->pushButton_jichuang->setStyleSheet(QPushButton_jichuang_STYTLE_CLOSE);
        ui->pushButton_jichuang->setText("机床关");
        ui->pushButton_jichuang->node.data_type="0";
    }
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_jichuang->node.type=2;
    ui->pushButton_jichuang->node.key=8;
    node_data.insert("type",ui->pushButton_jichuang->node.type);
    node_data_data.insert("key",ui->pushButton_jichuang->node.key);
    node_data_data.insert("val",ui->pushButton_jichuang->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}

void MyHomePage::pushButtonFan()
{
    if(ui->pushButton_Fan->isChecked())
    {
        ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_OPEN);
        ui->pushButton_Fan->setText("风扇开");
        ui->pushButton_Fan->node.data_type="1";
    }
    else if(!ui->pushButton_Fan->isChecked())
    {
        ui->pushButton_Fan->setStyleSheet(QPushButton_Fan_STYTLE_CLOSE);
        ui->pushButton_Fan->setText("风扇关");
        ui->pushButton_Fan->node.data_type="0";
    }
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_Fan->node.type=2;
    ui->pushButton_Fan->node.key=7;
    node_data.insert("type",ui->pushButton_Fan->node.type);
    node_data_data.insert("key",ui->pushButton_Fan->node.key);
    node_data_data.insert("val",ui->pushButton_Fan->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}

void MyHomePage::pushButtonjingbao()
{
    if(ui->pushButton_jingbao->isChecked())
    {
        ui->pushButton_jingbao->setStyleSheet(QPushButton_jingbao_STYTLE_OPEN);
        ui->pushButton_jingbao->setText("警报开");
        ui->pushButton_jingbao->node.data_type="1";
        connect(this,SIGNAL(ringSignal(int)),this,SLOT(ringSlot(int)));
    }
    else if(!ui->pushButton_jingbao->isChecked())
    {
        ui->pushButton_jingbao->setStyleSheet(QPushButton_jingbao_STYTLE_CLOSE);
        ui->pushButton_jingbao->setText("警报关");
        ui->pushButton_jingbao->node.data_type="0";
        QPixmap pixmap(":/new/prefix1/ringcutoff.png");
        ui->ring->setPixmap(pixmap);
        ui->ring->setScaledContents(true);
        disconnect(this,SIGNAL(ringSignal(int)),this,SLOT(ringSlot(int)));
    }
    QJsonObject node_data;
    QJsonObject node_data_data;
    QJsonDocument document;
    ui->pushButton_jingbao->node.type=2;
    ui->pushButton_jingbao->node.key=2;
    node_data.insert("type",ui->pushButton_jingbao->node.type);
    node_data_data.insert("key",ui->pushButton_jingbao->node.key);
    node_data_data.insert("val",ui->pushButton_jingbao->node.data_type);
    node_data.insert("data",QJsonValue(node_data_data));
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);
}

void MyHomePage::ringSlot(int type)
{
    if(type==0)
    {
        QPixmap pixmap(":/new/prefix1/ringcutoff.png");
        ui->ring->setPixmap(pixmap);
        ui->ring->setScaledContents(true);
    }
    if(type==1)
    {
        QPixmap pixmap(":/new/prefix1/ringshow.png");
        ui->ring->setPixmap(pixmap);
        ui->ring->setScaledContents(true);
    }
}
void MyHomePage::fireSlot(int type)
{
    if(type==0)
    {

        QPixmap pixmap(":/new/prefix1/huomie.png");
        ui->fire->setPixmap(pixmap);
        ui->fire->setScaledContents(true);
    }
    if(type==1)
    {
        QPixmap pixmap(":/new/prefix1/kaihuo.png");
        ui->fire->setPixmap(pixmap);
        ui->fire->setScaledContents(true);
    }
}

void MyHomePage::flashSlot()
{

    QJsonObject node_data;
    QJsonDocument document;
    ui->pushButton_flash->node.type=1;
    ui->pushButton_flash->node.limit="all";
    node_data.insert("type",ui->pushButton_flash->node.type);
    node_data.insert("limit",ui->pushButton_flash->node.limit);
    document.setObject(node_data);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    QMQTT::Message message;
    message.setPayload(bytearray);
    message.setTopic("/app/data/down");
    GlobalVal::mqtt->publish(message);

}
